<template>
  <div class="reportMistakeInfoActionEditor" >
    <el-dialog :title="dialogTitle" :visible="true" :gutter="24" @close="close">
      <el-form :gutter="24" :model="param" :rules="rules" ref="reportMistakeInfoActionEditor" :inline="true"  label-width="130px" class="demo-ruleForm">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="线路：" prop="lineCode">
              <el-select v-model="param.lineCode" placeholder="请选择" :disabled="showType==-1" @change="changeLineCode" filterable>
                <el-option
                  v-for="(item,index) in lineCodes"
                  :key="index"
                  :label="item.name"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车站：" prop="stationCode">
              <el-select v-model="param.stationCode" placeholder="请选择车站" :disabled="showType==-1" @change="changeStationCode" filterable>
                <el-option
                  v-for="(item,index) in stationCodes"
                  :key="index"
                  :label="item.name"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="报表时间：" prop="reprotTime">
              <el-date-picker type="date" placeholder="报表时间" v-model="param.reprotTime" style="width: 100%;" :disabled="showType==-1" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="reportNameId" label="报表名称：">
              <el-select v-model="param.reportNameId" placeholder="报表名称" :disabled="showType==-1">
                <el-option
                  v-for="(item,index) in reportNameIds"
                  :key="index"
                  :label="item.reportName"
                  :value="item.id+''">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="值班站长：" prop="dutyStationLeaderId">
              <el-select v-model="param.dutyStationLeaderId" placeholder="值班站长" filterable :disabled="showType==-1" @change="(v)=>getUserCode( v ,dutyStationLeaderIds,'dutyStationLeaderCode')">
                <el-option
                  v-for="(item,index) in dutyStationLeaderIds"
                  v-if="item!=null"
                  :key="index"
                  :label="item.nikename"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工号：" prop="dutyStationLeaderCode">
              <el-input type="text" v-model="param.dutyStationLeaderCode" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="值班员：" prop="dutyStationPersonId">
              <el-select v-model="param.dutyStationPersonId" placeholder="值班员" filterable :disabled="showType==-1" @change="(v)=>getUserCode(v,dutyStationPersonIds,'dutyStationPersonCode')">
                <el-option
                  v-for="(item,index) in dutyStationPersonIds"
                  v-if="item!=null"
                  :key="index"
                  :label="item.nikename"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工号：" prop="dutyStationPersonCode">
              <el-input type="text" v-model="param.dutyStationPersonCode" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="站务员：" prop="stationPersonId">
              <el-select v-model="param.stationPersonId" filterable placeholder="站务员" :disabled="showType==-1" @change="(v)=>getUserCode(v,stationPersonIds,'stationPersonCode')">
                <el-option
                  v-for="(item,index) in stationPersonIds"
                  v-if="item!=null"
                  :key="index"
                  :label="item.nikename"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工号：" prop="stationPersonCode">
              <el-input type="text" v-model="param.stationPersonCode" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" v-if="false">
          <el-col :span="12">
            <el-form-item label="结算开始日期：" prop="settleBeginTime">
              <el-date-picker type="date" placeholder="结算开始日期" v-model="param.settleBeginTime" style="width: 100%;" :disabled="showType==-1" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结算结束日期：" prop="settleEndTime">
              <el-date-picker type="date" placeholder="结算结束日期" v-model="param.settleEndTime" style="width: 100%;" :disabled="showType==-1" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="错误详情：" prop="errorDetail" style="width: 90%">
              <el-input style="width: 90%" type="textarea" v-model="param.errorDetail" :disabled="showType==-1"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" style="text-align: center">
            <el-form-item>
              <el-button type="primary" @click="handleSubmit()" v-if="showType==0">确认</el-button>
              <el-button type="primary" @click="updateReport()" v-if="showType==1">确认</el-button>
              <el-button @click="close()" style="margin-left: 30px" v-if="showType==-1">返回</el-button>
            </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>


<script>
    import {getForm, postForm} from '@/api/gen/gen'
    import moment from "moment";
    import {
      addReportError,
      getLineLists,
      getPersonRole,
      getReports,
      getStationLists, updateReport
    } from "../../../../api/subwayRat/reportMondify/reportMistakeInfo";
    import {getStationByCode, getSysUserToLS} from "../../../../api/subwayRat/commonCheck";

    export default {
        data() {
            return {
              showType:0,//0添加，1编辑，-1预览
              dialogTitle:'', //弹框标题
              lineCodes:[],
              stationCodes:[],
              reportNameIds:[],
              dutyStationLeaderIds:[],
              dutyStationPersonIds:[],
              stationPersonIds:[],
              param:{
                id:'',
                lineCode:"",
                stationCode:'',
                reprotTime:"",
                reportNameId:"",
                dutyStationLeaderId:"",
                dutyStationLeaderCode:"",
                dutyStationPersonId:"",
                dutyStationPersonCode:"",
                stationPersonId:"",
                stationpPersonCode:"",
                settleBeginTime:"",
                settleEndTime:"",
                errorDetail:'',
                cardNo: ''
              },
              rules: {
                // dutyStationLeaderId: [
                //   { required: true, message: '非空', trigger: 'blur' }
                // ],
                // dutyStationPersonId: [
                //   { required: true, message: '非空', trigger: 'blur' }
                // ],
                stationPersonId: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                lineCode: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                stationCode: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                reprotTime: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                reportNameId: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                // settleBeginTime: [
                //   { required: true, message: '非空', trigger: 'blur' }
                // ],
                // settleEndTime: [
                //   { required: true, message: '非空', trigger: 'blur' }
                // ],
                errorDetail: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
              }
            }
        },
        created() {
          this.row = this.$route.query.row;
          console.log(this.row);
          if(this.row!=undefined) {
            this.$nextTick(function () {
              if (this.row.reportNameId != undefined && this.row.reportNameId != '') {
                this.param.reportNameId = this.row.reportNameId;
              }
              if (this.row.inputTime != undefined && this.row.inputTime != '') {
                this.param.reprotTime = this.row.inputTime;//(new Date().getFullYear() + "-" + (new Date().getMonth() + 1) + "-" + new Date().getDate()) + " " + new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds();
              }else{
                this.param.reprotTime = (new Date().getFullYear() + "-" + (new Date().getMonth() + 1) + "-" + new Date().getDate()) + " " + new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds();
              }
            });
          }else {
            this.$nextTick(function () {
              if (this.param.reprotTime == undefined || this.param.reprotTime == '') {
                this.param.reprotTime = (new Date().getFullYear() + "-" + (new Date().getMonth() + 1) + "-" + new Date().getDate()) + " " + new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds();
              }
            });
          }
        },
        methods: {
          moment,
          handleSubmit() {
            let th=this;
            this.$refs.reportMistakeInfoActionEditor.validate((valid) => {
              if (valid) {
                th.param.reportTime=moment().format('YYYY-MM-DD HH:mm:ss');
                th.param.userCode = th.$store.getters.userInfo.userCode;
                addReportError(this.param).then((res)=>{
                  this.close();
                  this.$message({
                    message: res.data.code==0?'操作成功':'操作失败',
                    type: res.data.code==0?'success':'error',
                  });
                  this.$parent.search();
                })
              } else {
                return false;
              }
            });
          },
          async updateReport(){
            await updateReport(this.param).then((res)=>{
              this.$message({
                message: res.data.code==0?'操作成功':'操作失败',
                type: res.data.code==0?'success':'error',
              });
            })
            this.$parent.search();
            this.close();
          },
          previewEditor(row){
            this.editorContent(row);
          },
          editorContent(row){
            Object.assign(this.param,row);
            let p={};
            p.userCode=this.$store.getters.userInfo.userCode;
            getLineLists(p).then((res)=>{
              this.lineCodes=res.data.data;
            });
            if(row.stationCode!=''){
              this.changeLineCode(row.lineCode,true);
            }
            if(row && this.showType!=0 ){
              this.changeLineCode(row.lineCode,true);
              this.changeStationCode(row.stationCodes);
            }
          },
          /**
           *
           * @param v 线路，是否展示车站框
           * @param showSt
           */
          changeLineCode(v,showSt){
            this.row = this.$route.query.row;
            let p={};
            let param='';
            p.userCode=this.$store.getters.userInfo.userCode;
            p.lineCode=v;
            if(this.row=='' || this.row==undefined)
              param=v;
            else
              param=this.row.submitLineCode;
            // getStationLists(p.lineCode).then((res)=>{
            //   this.stationCodes=res.data.data;
            getStationByCode({line_code:param}).then(res=>{
              this.stationCodes=res.data;
              console.log(res)
              // if(!showSt){
              //   this.param.stationCode='';
              // }
            })
          },
          changeStationCode(v){
            getSysUserToLS({lineCode:this.param.lineCode,stationCode:v}).then(res=>{
              this.dutyStationLeaderIds=res.data.data;
              this.dutyStationPersonIds=res.data.data;
              this.stationPersonIds=res.data.data;
            })
            // getPersonRole({roleCode:'ZBZZ',lineCode:this.param.lineCode,stationCode:v}).then((res)=>{
            //   this.dutyStationLeaderIds=res.data.data;
            // });
            // getPersonRole({roleCode:'KYZBY',lineCode:this.param.lineCode,stationCode:v}).then((res)=>{
            //   this.dutyStationPersonIds=res.data.data;
            // });
            // getPersonRole({roleCode:'ZHANWURENYUAN',lineCode:this.param.lineCode,stationCode:v}).then((res)=>{
            //   this.stationPersonIds=res.data.data;
            // });
          },
          async initReportNameIds(){
            await getReports({}).then((res)=>{
              this.reportNameIds=res.data.data;
            })
          },
          getUserCode(id,users,codeVar){
            console.log(users,id,codeVar);
            for(let i=0;i<users.length;i++){
              if(users[i].userId==id){
                this.param[codeVar]=users[i].userCode;
              }
            }
          },
          close(){
            this.$parent.isShowReportMistakeInfoEditor=false;
          },
          clearParam(){
            this.param={
              id:'',
              lineCode:"",
              stationCode:'',
              reprotTime:"",
              reportNameId:"",
              dutyStationLeaderId:"",
              dutyStationLeaderCode:"",
              dutyStationPersonId:"",
              dutyStationPersonCode:"",
              stationPersonId:"",
              stationpPersonCode:"",
              settleBeginTime:"",
              settleEndTime:"",
              errorDetail:'',
              cardNo:''
            };
          }
        },
      mounted() {
          // this.editorContent();
        this.initReportNameIds();
      }
    }
</script>
<style lang="scss">
  .reportMistakeInfoActionEditor{
    .el-form-item{
      .el-form-item__content{
        width:190px;
        .el-textarea__inner{
          width:560px;
          height: 80px;
        }
      }
    }
    .el-dialog__wrapper .el-dialog{
      width: 800px;
    }
  }
</style>
